package com.per.note.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.per.note.bean.User;
import com.per.note.ui.load.MyApplication;

import java.util.List;
import java.util.Random;


public class UserDao {
	private Context context;
	SqliteManage sqliteManage = SqliteManage.getInstance(context);

	public UserDao(Context context) {
		this.context = context;
	}
	
	/**
	 * 添加一条用户记录
	 */
	public void add(String name, String pwd) {
		ContentValues contentValues = new ContentValues();
		Random random = new Random();
		int id = random.nextInt(1000);
//		MyApplication.userid = id;
		contentValues.put("userid",Integer.valueOf(id));
		contentValues.put("name",name);
		contentValues.put("password",pwd);
		contentValues.put("islogin",false);
		boolean result = sqliteManage.insertItem("user",contentValues);
	}

	/**
	 * 删除一条记录
	 */
	public void delete(String name) {
		sqliteManage.deleteItem("user","name=?",new String[]{name});
	}

	/**
	 * 数据库的更改操作
	 * 修改帐户名 密码
	 */
	public void update(String name, String newname, String newpwd) {
		ContentValues values = new ContentValues();
		values.put("name", newname);
		values.put("possward", newpwd);
		values.put("islogin", false);
		sqliteManage.updateItem("user","name=?",new String[]{newname},values);
	}
	/**
	 * 数据库的更改操作
	 * 修改登录状态为已登录
	 */
	public void updateLoginOK(int userid) {
		ContentValues values = new ContentValues();
		values.put("islogin", true);
		sqliteManage.updateItem("user","userid=?",new String[]{String.valueOf(userid)},values);
	}
	
	/**
	 * 数据库的更改操作
	 * 修改登录状态为未登录
	 */
	public void updateLoginCancel(String name) {
		ContentValues values = new ContentValues();
		values.put("islogin", false);
		sqliteManage.updateItem("user","name=?",new String[]{name},values);
	}

	/**
	 * 数据库的查询操作 判断有无该数据
	 * 判断用户是否存在
	 */
	public boolean find(String name) {
		SqliteManage.QueryResult queryResult;
		queryResult = sqliteManage.query("user", "name=?", new String[]{name});
		if (queryResult.cursor.moveToFirst())
			return true;
		return false;
	}

	/**
	 * 数据库的查询操作 判断有无该数据
	 * 判断登录结果
	 */
	public boolean findLogin(String name, String pwd) {
		SqliteManage.QueryResult query;
		query = sqliteManage.query("user", "name=? and password=?", new String[]{name, pwd});
		if (query.cursor.moveToFirst()){
			MyApplication.userid = query.cursor.getInt(query.cursor.getColumnIndex("userid"));
			return true;
		}
		return false;
	}
	
	
	/**
	 * 查询所有信息
	 */
	public List<User> findAll() {
		List<User> userList = null;
		SqliteManage.QueryResult queryResult = sqliteManage.query("user", "userid=?", new String[]{String.valueOf(MyApplication.userid)});
		Cursor cursor = queryResult.cursor;
		while (cursor.moveToNext()){
			User user = new User();
			String name = cursor.getString(cursor.getColumnIndex("name"));
			user.setName(name);
			String pwd = cursor.getString(cursor.getColumnIndex("password"));
			user.setPassword(pwd);
			String islogin = cursor.getString(cursor.getColumnIndex("islogin"));
			user.setIslogin(Boolean.valueOf(islogin));
			userList.add(user);
		}
		cursor.close();
		return userList;
	}

	/**
	 * 查询所有信息
	 */
//	public Cursor findAllbyCursor() {
//		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
//		if (db.isOpen()) {
//			/*Cursor cursor = db.query("person", null, null, null, null, null,
//					null);*/
//			Cursor cursor = db.rawQuery("select personid as _id,name from person", null);
//			cursor.close(); //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//			return cursor;
//			//注意一定不要把数据库关闭了
//			}
//		return null;
//
//	}

	/**
	 * 查询登录是否成功
     */
	public User findLoginOk() {
		User user = new User();
		Log.i("------","查表，登录成功的User信息");
		SqliteManage.QueryResult queryResult = sqliteManage.query("user", "islogin=1 and userid=?", new String[]{String.valueOf(MyApplication.userid)});
		Cursor cursor = queryResult.cursor;
		if(cursor.moveToNext()){
			String name = cursor.getString(cursor.getColumnIndex("name"));
			user.setName(name);
			String pwd = cursor.getString(cursor.getColumnIndex("password"));
			user.setPassword(pwd);
			String islogin = cursor.getString(cursor.getColumnIndex("islogin"));
			user.setIslogin(Boolean.valueOf(islogin));
		}
		cursor.close();
		return user;
	}

}
